package cn.hzking.gjmall.net;

import android.content.Context;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import cn.hzking.gjmall.app.GjAppUtils;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.constant.Constants;
import cn.hzking.gjmall.net.config.HttpConfig;
import cn.hzking.gjmall.net.cookie.CookieManger;
import cn.hzking.gjmall.net.intercept.InterceptorUtil;
import cn.hzking.gjmall.net.interfaces.ApiInterface;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2018/1/20.
 */

public class RetrofitOssFactory {
    private final ApiInterface apiInterface;
    private static Context context;
    private RetrofitOssFactory(Context context){
        OkHttpClient mOkHttpClient=new OkHttpClient.Builder()
                .cookieJar(new CookieManger(context))//持久化cookie
                .connectTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置写入超时时间
                .addInterceptor(InterceptorUtil.tokenInterceptor(context))//添加其他拦截器
                .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
           //     .sslSocketFactory(HttpsFfactory.getSSLSocketFactory(context,0))//添加https证书(第二个参数是)
              //  .hostnameVerifier(HttpsFfactory.getHostnameVerifier("")) //过滤url,添加hostName验证器
                .build();

        Retrofit mRetrofit=new Retrofit.Builder()
                .baseUrl(Constants.isDebug? Gjmall.getDeBugOssHost():Gjmall.getOssHost())
                .addConverterFactory(GsonConverterFactory.create())//添加gson转换器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
                .client(mOkHttpClient)
                .build();

        apiInterface = mRetrofit.create(ApiInterface.class);
    }

    private static final class Holder{
        private static final Context context = GjAppUtils.getContext();
        private static final RetrofitOssFactory RETROFITFACTORY = new RetrofitOssFactory(context);
    }
    public static final RetrofitOssFactory getInstant(){
        return Holder.RETROFITFACTORY;
    }

    public ApiInterface API(){
        return apiInterface;
    }



}
